package org.jeecgframework.web.service.impl;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.jeecgframework.core.util.ResourceUtil;
import org.jeecgframework.core.util.oConvertUtils;
import org.jeecgframework.web.service.CbsAppKnowledgeServiceI;
import org.jeecgframework.web.system.pojo.base.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("cbsAppKnowledgeService")
@Transactional
public class CbsAppKnowledgeServiceImpl extends CommonServiceImpl implements CbsAppKnowledgeServiceI {

	
 	public <T> void delete(T entity) {
 		super.delete(entity);
 		//执行删除操作配置的sql增强
		this.doDelSql((CbsAppKnowledgeEntity)entity);
 	}
 	
 	public <T> Serializable save(T entity) {
 		Serializable t = super.save(entity);
 		//执行新增操作配置的sql增强
 		this.doAddSql((CbsAppKnowledgeEntity)entity);
 		return t;
 	}
 	
 	public <T> void saveOrUpdate(T entity) {
 		super.saveOrUpdate(entity);
 		//执行更新操作配置的sql增强
 		this.doUpdateSql((CbsAppKnowledgeEntity)entity);
 	}
 	
 	/**
	 * 默认按钮-sql增强-新增操作
	 * @return
	 */
 	public boolean doAddSql(CbsAppKnowledgeEntity t){
	 	return true;
 	}
 	/**
	 * 默认按钮-sql增强-更新操作
	 * @return
	 */
 	public boolean doUpdateSql(CbsAppKnowledgeEntity t){
	 	return true;
 	}
 	/**
	 * 默认按钮-sql增强-删除操作
	 * @return
	 */
 	public boolean doDelSql(CbsAppKnowledgeEntity t){
	 	return true;
 	}
 	
 	/**
	 * 替换sql中的变量
	 * @param sql
	 * @return
	 */
 	public String replaceVal(String sql,CbsAppKnowledgeEntity t){
 		sql  = sql.replace("#{id}",String.valueOf(t.getId()));
 		sql  = sql.replace("#{no}",String.valueOf(t.getNo()));
 		sql  = sql.replace("#{title}",String.valueOf(t.getTitle()));
 		sql  = sql.replace("#{content}",String.valueOf(t.getContent()));
 		sql  = sql.replace("#{create_name}",String.valueOf(t.getCreateName()));
 		sql  = sql.replace("#{create_by}",String.valueOf(t.getCreateBy()));
 		sql  = sql.replace("#{create_date}",String.valueOf(t.getCreateDate()));
 		sql  = sql.replace("#{update_name}",String.valueOf(t.getUpdateName()));
 		sql  = sql.replace("#{update_by}",String.valueOf(t.getUpdateBy()));
 		sql  = sql.replace("#{update_date}",String.valueOf(t.getUpdateDate()));
 		sql  = sql.replace("#{UUID}",UUID.randomUUID().toString());
 		return sql;
 	}

	@Override
	public List<Map<String, Object>> queryAppKnowledge(HttpServletRequest req) {
		String condition = oConvertUtils.getString(req.getParameter("condition"));
		TSUser user = ResourceUtil.getSessionUserName();
		user = super.getEntity(TSUser.class, user.getId());
		String userId = "";
		if(null == user){
			req.getSession().setAttribute("lang", "zh-cn");
			String _webkit_uid = oConvertUtils.getString(req.getParameter("_webkit_uid"));
			user = super.getEntity(TSUser.class, _webkit_uid);
			userId =  user.getId ();
		}else{
			userId =  user.getId ();
		}
		List<TSRoleUser> userOrgList = super.findHql("from TSRoleUser where TSUser.id = ?", userId );
		String sql = " SELECT id, title, no , DATE_FORMAT(CREATE_DATE,'%Y-%m-%d %H:%i:%s') as createDate FROM cbs_app_knowledge WHERE 1=1 ";
		if(!"".equals(condition)){
			sql += "and (content LIKE '%"+condition+"%' or title LIKE '%"+condition+"%' OR zy_es_item LIKE '%"+condition+"%' ";

			List<TSType> types = this.findByPropertyLike(TSType.class, "typename", condition);
			if(types.size()>0){
				for (TSType type : types) {
					sql+=" or know_ledge_type like '%"+type.getTypecode()+"%' ";
				}
			}

			sql+=") ";
		}
		if(userOrgList.size ()>0){
			for (int i = 0; i < userOrgList.size (); i++) {
				String roleId = userOrgList.get ( i ).getTSRole().getId ();
				if(i==0){
					sql+="and roleid LIKE '%"+roleId+"%'";
				}else{
					sql+="or roleid LIKE '%"+roleId+"%'";
				}
			}
		}

		return super.findForJdbc(sql);
	}

	@Override
	public Map<String, Object> queryKnowledgeById(String id) {
		StringBuilder sql = new StringBuilder();
		sql.append(" SELECT ");
		sql.append(" 	ak.title, ak.no, ak.content ,DATE_FORMAT(CREATE_DATE,'%Y-%m-%d %H:%i:%s') as createDate ");
		sql.append(" FROM ");
		sql.append(" 	cbs_app_knowledge ak");
		sql.append(" WHERE ak.id ='").append(id).append("' ");
		Map<String, Object> map = super.findOneForJdbc(sql.toString()); 
		map.put("content", map.get("content").toString().replace("\r\n", "<br>"));
		return map;
	}
}